/**
 * Copyright 2014 wasu.com
 *
 *
 * Create on 2014-8-14 上午11:23:52
 */
package xiaojian.toolkit.handler.actor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import xiaojian.toolkit.handler.AbstractCommonBean;
import xiaojian.toolkit.handler.ReqHandler;
import xiaojian.toolkit.util.ClosureEx;

import com.google.common.base.Preconditions;

/**
 * reqHanler直接调用doReq方法，写死即可。
 * 
 * @author <a href="mailto:caoxiaojian@wasu.com>xiaojian.cao</a>
 * 
 */
public class ReqHandlerFunctor implements ClosureEx {

	private static final Logger logger = LoggerFactory
			.getLogger(ReqHandlerFunctor.class);

	private boolean canceled = false;
	private ReqHandler<AbstractCommonBean> reqHandler = null;

	public ReqHandlerFunctor(ReqHandler<AbstractCommonBean> reqHandler) {
		Preconditions.checkArgument(reqHandler != null);

		this.reqHandler = reqHandler;
	}

	public void execute(Object... args) {
		if (!canceled) {
			for (Object obj : args) {
				logger.debug("Using handler[{}] process bean[{}].", reqHandler,
						obj);
				reqHandler.doReq((AbstractCommonBean) obj);
			}
		}
	}

	/**
	 * @return the canceled
	 */
	public boolean isCanceled() {
		return canceled;
	}

	/**
	 * @param canceled
	 *            the canceled to set
	 */
	public void setCanceled(boolean canceled) {
		this.canceled = canceled;
	}

	/**
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		sb.append(this.reqHandler);
		sb.append(".");

		if (this.canceled) {
			sb.append("[canceled]");
		}
		return sb.toString();
	}
}
